package com.example.zhihuigongdi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zhihuigongdi.commer.CommonException;
import com.example.zhihuigongdi.entity.Cameras;
import com.example.zhihuigongdi.entity.Machinery;
import com.example.zhihuigongdi.entity.User;
import com.example.zhihuigongdi.entity.respon.Result;
import com.example.zhihuigongdi.mapper.MachineryMapper;
import com.example.zhihuigongdi.service.IMachineryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhihuigongdi.utils.CheckUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuhongbin
 * @since 2024-04-17
 */
@Service
public class MachineryServiceImpl extends ServiceImpl<MachineryMapper, Machinery> implements IMachineryService {

    @Autowired
    private CheckUtil checkUtil;

    @Autowired
    @Lazy
    private IMachineryService machineryService;
    @Override
    public Result addMachinery(Machinery machinery) {
        User user = checkUtil.getUser();
        checkUtil.checkUser(user);

        if (!user.getUserRole().equals("admin")){
            throw new CommonException("权限不足");
        }

        machineryService.save(machinery);
        return Result.success("添加成功");
    }

    @Override
    public Result removeMachinery(int machineryId) {
        User user = checkUtil.getUser();
        checkUtil.checkUser(user);

        if (!user.getUserRole().equals("admin")){
            throw new CommonException("权限不足");
        }

        QueryWrapper<Machinery> machineryQueryWrapper = new QueryWrapper<>();
        machineryQueryWrapper.eq("machinery_id",machineryId);
        machineryService.remove(machineryQueryWrapper);

        return Result.success("删除成功");
    }

    @Override
    public Result updateStatus(Machinery machinery) {
        User user = checkUtil.getUser();
        checkUtil.checkUser(user);

        if (!user.getUserRole().equals("admin")){
            throw new CommonException("权限不足");
        }
        QueryWrapper<Machinery> machineryQueryWrapper = new QueryWrapper<>();
        machineryQueryWrapper.eq("machinery_id",machinery.getMachineryId());
        Machinery one = machineryService.getOne(machineryQueryWrapper);
        if (BeanUtil.isEmpty(one)){
            throw new CommonException("无该器械");
        }

        machineryService.updateStatus(machinery);
        return Result.success("修改成功");
    }

    @Override
    public Result getMachinery(int current, int size) {
        User user = checkUtil.getUser();
        checkUtil.checkUser(user);

        if (!user.getUserRole().equals("admin")){
            throw new CommonException("权限不足");
        }
        PageHelper.startPage(current,size);
        List<Machinery> list = machineryService.list();
        PageInfo<Machinery> machineryPageInfo = new PageInfo<>(list);
        return Result.success(machineryPageInfo);
    }
}
